#include "Plane.h"

namespace lib3dw
{
	Plane::Plane(Vector3 &v1,  Vector3 &v2,  Vector3 &v3)
	{
		set3Points(v1,v2,v3);
	}

	Plane::Plane()
	{
	}

	Plane::~Plane()
	{
	}

	void Plane::set3Points(Vector3 &v1,  Vector3 &v2,  Vector3 &v3)
	{
		Vector3 aux1, aux2;

		aux1 = v1 - v2;
		aux2 = v3 - v2;

		normal = cross(aux2, aux1);// aux2 * aux1;

		normal.normalize();
		point.x = v2.x;
		point.y = v2.y;
		point.z = v2.z;
		d = -(dot(normal, point));
	}

	void Plane::setNormalAndPoint(Vector3 &normal, Vector3 &point)
	{
		normal.x = this->normal.x;
		normal.y = this->normal.y;
		normal.z = this->normal.z;
		
		this->normal.normalize();
		d = -(dot(this->normal, point));
	}

	void Plane::setCoefficients(float a, float b, float c, float d)
	{
		normal.x = a;
		normal.y = b;
		normal.z = c;

		float l = normal.length();
		// normalize the vector
		normal.normalize();
		this->d = d/l;
	}

	float Plane::distance(Vector3 &p)
	{
		return (d + dot(normal, p));
	}
}